/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.actions.cambios;

import com.mapping.CambioComp;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
/**
 *
 * @author Andres
 */
@Namespace(value = "/")
@Action(value = "gestionCambios", results = {@Result(name = "success", location = "/GestionCambios/gestionCambios.jsp")})
public class GestionCambiosAction extends ActionSupport{
    private List<CambioComp> listaCambios;
    private String buscar;
    @Override
    public String execute(){
        String consulta1 = "select c.IDTICKET, c.IDCOMPONENTE,c.IDCOMPONENTECAMBIO from cambio_comp c "
                            + "inner join ticket t on (c.idTicket=t.idTicket) where t.estado!=?1 and t.tipoProblema=?2";
        String consulta2 = "select c.IDTICKET, c.IDCOMPONENTE,c.IDCOMPONENTECAMBIO from cambio_comp c "
                            + "inner join ticket t on (c.idTicket=t.idTicket) "
                            + "where match (t.idTicket,t.descripcion,t.empAtendio,t.EmpSolicito,t.prioridad,t.estado,t.EmpResolvio,t.tipoProblema,t.idDispositivo) "
                            + "against (?1 IN BOOLEAN MODE) AND t.estado!=?2 and t.tipoProblema=?3";
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("inventarios");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        if(buscar== null || buscar.equals("")){
            Query query =  em.createNativeQuery(consulta1,CambioComp.class);
            query.setParameter(1,"cerrado");
            query.setParameter(2,"RFC");
            listaCambios = (List<CambioComp>) query.getResultList();
        }else{
            Query query =  em.createNativeQuery(consulta2,CambioComp.class);
            query.setParameter(1,buscar);
            query.setParameter(2,"cerrado");
            query.setParameter(3,"RFC");
            listaCambios = (List<CambioComp>) query.getResultList();
        }
        tx.commit();
        em.close();
        return SUCCESS;
    }

    public void setBuscar(String buscar) {
        this.buscar = buscar;
    }

    public List<CambioComp> getListaCambios() {
        return listaCambios;
    }
}
